package com.guomei.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guomei.bean.Certificate;
import com.guomei.bean.User;
import com.guomei.bean.certificate.vo.CertficateDTO;
import com.guomei.bean.curse.Channel;
import com.guomei.mapper.CertificateMapper;
import com.guomei.mapper.UserMapper;
import com.guomei.service.CertificateService;
import com.guomei.utils.PageData;
import com.guomei.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class CertificateServiceImpl implements CertificateService {
    @Autowired
    private CertificateMapper certificateMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public Result<?> createCertificate(Certificate certificate) {
        try {
            int result = certificateMapper.insert(certificate);
            if (result > 0) {
                return Result.success("证书创建成功", certificate);
            } else {
                return Result.fail("证书创建失败");
            }
        } catch (Exception e) {
            log.error("创建证书失败", e);
            return Result.error("创建证书异常: " + e.getMessage());
        }
    }

    @Override
    public Result<?> selectByCondition(CertficateDTO certificateDto) {
        PageHelper.startPage(certificateDto.getPageNum(), certificateDto.getPageSize());
        Certificate certificate = new Certificate();
        certificate.setUserId(certificateDto.getUserId());
        certificate.setExamId(certificateDto.getExamId());
        certificate.setCertNo(certificateDto.getCertNo());
        certificate.setPdfUrl(certificateDto.getPdfUrl());

        List<Certificate> certificates = certificateMapper.selectByCondition(certificate);
        if (certificates != null && !certificates.isEmpty()){
            User user = userMapper.selectById(certificate.getUserId());
            certificate.setUser(user);
        }
        // 封装分页结果到PageData
        PageData<Certificate> pageData = new PageData<>();
        PageInfo<Certificate> pageInfo = new PageInfo<>(certificates);
        pageData.setTotal(pageInfo.getTotal());
        pageData.setData(pageInfo.getList());
        pageData.setPageNum(certificateDto.getPageNum());
        pageData.setPageSize(certificateDto.getPageSize());
        return Result.success(pageData);
    }

    @Override
    public Result<Certificate> getCertificateById(Long id) {
        try {
            Certificate certificate = certificateMapper.selectById(id);
            if (certificate != null) {
                return Result.success(certificate);
            } else {
                return Result.fail("证书不存在");
            }
        } catch (Exception e) {
            log.error("查询证书失败，ID: {}", id, e);
            return Result.error("查询证书异常: " + e.getMessage());
        }
    }

    @Override
    public Result<Certificate> getCertificateByUserIdAndExamId(Long userId, Long examId) {
        try {
            Certificate certificate = certificateMapper.selectByUserIdAndExamId(userId, examId);
            if (certificate != null) {
                return Result.success(certificate);
            } else {
                return Result.fail("证书不存在");
            }
        } catch (Exception e) {
            log.error("查询证书失败，userId: {}, examId: {}", userId, examId, e);
            return Result.error("查询证书异常: " + e.getMessage());
        }
    }

    @Override
    public Result<Certificate> getCertificateByCertNo(String certNo) {
        try {
            Certificate certificate = certificateMapper.selectByCertNo(certNo);
            if (certificate != null) {
                return Result.success(certificate);
            } else {
                return Result.fail("证书不存在");
            }
        } catch (Exception e) {
            log.error("查询证书失败，certNo: {}", certNo, e);
            return Result.error("查询证书异常: " + e.getMessage());
        }
    }

    @Override
    public Result<PageData<Certificate>> getAllCertificates(int pageNum, int pageSize) {
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<Certificate> certificates = certificateMapper.selectAll();
            PageInfo<Certificate> pageInfo = new PageInfo<>(certificates);

            PageData<Certificate> pageData = new PageData<>();
            pageData.setTotal(pageInfo.getTotal());
            pageData.setData(pageInfo.getList());
            pageData.setPageNum(pageNum);
            pageData.setPageSize(pageSize);

            return Result.success(pageData);
        } catch (Exception e) {
            log.error("查询证书列表失败", e);
            return Result.error("查询证书列表异常: " + e.getMessage());
        }
    }

    @Override
    public Result<PageData<Certificate>> getCertificatesByUserId(Long userId, int pageNum, int pageSize) {
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<Certificate> certificates = certificateMapper.selectByUserId(userId);
            PageInfo<Certificate> pageInfo = new PageInfo<>(certificates);

            PageData<Certificate> pageData = new PageData<>();
            pageData.setTotal(pageInfo.getTotal());
            pageData.setData(pageInfo.getList());
            pageData.setPageNum(pageNum);
            pageData.setPageSize(pageSize);

            return Result.success(pageData);
        } catch (Exception e) {
            log.error("查询用户证书列表失败，userId: {}", userId, e);
            return Result.error("查询用户证书列表异常: " + e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result<?> updateCertificate(Certificate certificate) {
        try {
            int result = certificateMapper.update(certificate);
            if (result > 0) {
                return Result.success("证书更新成功");
            } else {
                return Result.fail("证书更新失败");
            }
        } catch (Exception e) {
            log.error("更新证书失败，ID: {}", certificate.getId(), e);
            return Result.error("更新证书异常: " + e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result<?> deleteCertificate(Long id) {
        try {
            int result = certificateMapper.deleteById(id);
            if (result > 0) {
                return Result.success("证书删除成功");
            } else {
                return Result.fail("证书删除失败");
            }
        } catch (Exception e) {
            log.error("删除证书失败，ID: {}", id, e);
            return Result.error("删除证书异常: " + e.getMessage());
        }
    }
}
